import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class QueuePanel extends JPanel{
	//	Local variables
	private JLabel _activeLineLbl;
	private JList _cusInLine;
	private JComboBox _queueBox;
	private JPanel _topPanel;
	private JScrollPane _queueScrollPanel;
	private String[] _queueNumbers;
	
	//	Peer objects
	private StoreLogic _logic;

	public QueuePanel(StoreLogic aLogic)
	{
		super(new BorderLayout());
		_logic = aLogic;
		_queueNumbers = new String[_logic.getMaxSize()];
		for (int i = 0; i < _logic.getMaxSize(); i++)
		{
			_queueNumbers[i] = "Queue " + (i + 1);
		}
		initGUI();
	}

	private void initGUI()
	{
		_topPanel = new JPanel(new FlowLayout());
		_activeLineLbl = new JLabel("Active line:");
		_cusInLine = new JList(_logic.getInLine(0));
		_queueBox = new JComboBox(_queueNumbers);
		_queueBox.addActionListener(new QueueBoxChangeSelectedIndex());
		_queueScrollPanel = new JScrollPane(_cusInLine);
		_topPanel.add(_activeLineLbl);
		_topPanel.add(_queueBox);
		this.add(_topPanel,BorderLayout.NORTH);
		this.add(_queueScrollPanel,BorderLayout.CENTER);
	}

	private class QueueBoxChangeSelectedIndex implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) 
		{	
			updateQPanel();
		}
	}
	
	/**
	 * Used to update the Qpanel when the user closes or opens a Line
	 */
	public void updateQPanel()
	{	
		if (_logic.getLineStatus(_queueBox.getSelectedIndex()))
		{
			_cusInLine.setModel(_logic.getInLine(_queueBox.getSelectedIndex()));
			_cusInLine.setVisible(true);
		}
		else
			_cusInLine.setVisible(false);
	}

	/**
	 * Gets the active queue.
	 */
	public int getSelectedQueue()
	{
		return _queueBox.getSelectedIndex();
	}
}
